package com.kcht.sylyg08.ctrl;

import com.kcht.adp.common.Session;
import com.kcht.adp.common.log.Logger;
import com.kcht.adp.pagination.Page;
import com.kcht.adp.web.rs.AdpRest;
import com.kcht.sylyg08.ctrl.vo.ContainerVO;
import com.kcht.sylyg08.ctrl.vo.InventoryInfo;
import com.kcht.sylyg08.service.ContainerActionHandler;
import com.kcht.sylyg08.service.ContainerService;
import com.kcth.sylyg08.entity.Container;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.QueryParam;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class ContainerCtrl {

    @Inject
    private Logger logger;

    @Inject
    private ContainerService service;

    @Inject
    private ContainerActionHandler containerActionHandler;

    @Inject
    private Session session;

    @AdpRest()
    public void save(Container container) {
        service.save(container);
    }

    @AdpRest(principal = "料框列表", permission = "料框列表")
    public Page<ContainerVO, ContainerVO> list(Page<ContainerVO, ContainerVO> page) {
        return service.list(page);
    }

    @AdpRest(principal = "删除料框", permission = "删除料框")
    public void delete(@QueryParam("ids") List<Integer> ids) {
        service.delete(ids);
    }

    @AdpRest(principal = "异常料框处理", permission = "异常料框处理")
    public void handleErrorContainer(ContainerVO vo) {
        containerActionHandler.errorContainerHandle(vo.getContainerId(), vo.getStatus(), session.getUser().getRealName(), vo.getDetails());
//        containerStatusService.changeContainerStatusByIntervene(vo.getContainerId(), vo.getStatus(), session.getUser().getRealName(), vo.getDetails());
    }

    @AdpRest(principal = "添加料框", permission = "添加料框")
    public Object importByExcel(Map<String, Object> obj) throws Exception {
        logger.info("接受excel文件。");
        File file = File.createTempFile("temp", ".xls");
        InputStream is = (InputStream) obj.remove("file");
        int index;
        byte[] buffer = new byte[1024 * 10];
        FileOutputStream fos = new FileOutputStream(file);
        while ((index = is.read(buffer, 0, 10 * 1024)) != -1) {
            fos.write(buffer, 0, index);
        }
        String msg = service.importContainerByExcel(file);
        is.close();
        fos.close();
        HashMap<String, String> res = new HashMap<>();
        res.put("msg", msg);


        /*UploadVo vo = BeanUtils.covert(obj, UploadVo.class);
        vo.setFile(file);
        User loginUser = subjectManager.getLoginUser();

        Map<String, Object> map = new HashMap<>();
        map.put("id", vo.getName());


        IWebImageManager.MetaData metadata = new IWebImageManager.MetaData();
        metadata.setCreateDate(new Date());
        metadata.setName(vo.getName());
        metadata.setSize(vo.getSize());
        metadata.setType(vo.getType());
        String uri = imageManager.upload(vo.getFile(), Objects.nonNull(loginUser) ? loginUser.getId().toString() : "anonymous", metadata);
        vo.getFile().close();
        map.put("fileId", uri);


//        imageManager.upload(,Objects.nonNull(loginUser)?loginUser.getId().toString():"anonymous");
        map.put("name", vo.getName());*/


        return res;
    }

    @AdpRest(principal = "库存信息", permission = "库存信息")
    public List<InventoryInfo> getInventoryInfo() {
        return service.getInventoryInfo();
    }
}
